﻿using HXDNB.DAL;
using HXDNB.Infrastructure;
using System;
using System.Linq;
using System.Web.Mvc;

namespace HXDNB.CMS.Controllers
{
    public class AdController : BaseController
    {
        //
        // GET: /Ad/ 

        public ViewResult Index(int index = 0)
        {
            using (var ab = new AdBLL())
            {
                ViewBag.PagList = new Paging<Advertisement>(index, ab.Select().AsQueryable());
            }

            return View();
        }

        /// <summary>
        /// 广告信息
        /// </summary>
        public ViewResult Info(int? id)
        {
            try
            {
                if (id.HasValue)
                {
                    using (var ab = new AdBLL())
                        return View(ab.Get(id.Value));
                }
            }
            catch (Exception ex)
            {
                Infrastructure.Log.Error("AdController.Info", ex.Message + ex.StackTrace);
            }

            return View(new Advertisement { IsRelease = "N" });
        }

        /// <summary>
        /// 广告信息
        /// </summary>
        [HttpPost]
        public ViewResult Info(Advertisement model)
        {
            try
            {
                using (var ab = new AdBLL())
                {
                    model.IsRelease = "N";
                    if (model.Id > 0)
                        ViewBag.Result = ab.Edit(model);
                    else
                        ViewBag.Result = ab.Add(model) > 0;
                }
            }
            catch (Exception ex)
            {
                ViewBag.Result = false;
                Infrastructure.Log.Error("AdController.Info[HttpPost]", ex.Message + ex.StackTrace);
            }

            return View();
        }

        /// <summary>
        /// 删除广告
        /// </summary>
        public ContentResult Delete(int id)
        {
            try
            {
                using (var ab = new AdBLL())
                    ab.Delete(id);
                return Content("Ok");
            }
            catch (Exception ex)
            {
                Infrastructure.Log.Error("AdController.Delete", ex.Message + ex.StackTrace);
                return Content("Error");
            }
        }
    }
}
